package practice;

class LinkedList<T> {

    private int  count=0 ;//链表的长度
    private Node root;//头结点

    public class Node{
        public   T data;
        public Node next;
        public Node(T value)
        {
            this.data = value;
            next = null;
        }

    }
//    头插法
    public  void heedInsert(T data)
    {
        Node newNode = new Node(data);
        if(root==null)
        {
            root = newNode;
            count++;
            return;
        }
        newNode.next = root;
        root = newNode;
        count++;

    }
//    尾插法
   public void tailInsert(T data)
   {
       Node node = new Node(data);
       if(root==null)
       {
           root =node;
           count++;
           return;
       }
       Node last = root;
       while (last.next!=null)
       {
           last = last.next;
       }
       last.next = node;
       count++;
   }
   public  int getCount()
   {
       return  this.count;
   }
//   指定位置插入
    public  void insertByIndex(T data ,int index)
    {
        Node node = new Node(data);
        if(index<0||index>count)
        {
            System.out.println("位置有问题，无法插入");
            return;
        }
        if(index==0)
        {
            heedInsert(data);
            return;

        }
//        if(index ==count)
//        {
//            tailInsert(data);
//            return;
//        }
        Node front = root;
        Node behind = root.next;
        int i =2;
        while(i<index)
        {
            front=behind;
            behind = behind.next;
            i++;
        }
        front.next = node;
        node.next = behind;
        count++;

    }
//删除节点
    public void delete(int index)
    {
        if(index<0&&index>count)
        {
            System.out.println("位置有问题，请重新选择");
            return;
        }
        Node front = root;
        Node behind = root.next;
        int i = 2 ;
        if(index == 0)
        {
            root = root.next;
            count--;
            return;
        }
        while (i<index)
        {
            front = behind;
            behind = behind.next;
        }
        front.next = behind.next;
        count--;
        return;

    }

//链表反转
    public void reverse()
    {
        Node cur  = root;
        Node front = null;
        Node temp;
        while (cur!=null)
        {
            temp = cur.next;
            cur.next = front;

            front = cur;
            cur = temp;
        }
        root = front;
    }
    public void all()
    {
        Node node = root;
        while (node!=null)
        {
            System.out.println(node.data);
            node = node.next;
        }
    }

}
class list
{
    public static void main(String[] args) {
//        LinkedList<String> list = new LinkedList<>();
//        list.heedInsert("黄开明");
//        list.tailInsert("黄婷婷");
//        list.insertByIndex("hhh",2);
//        System.out.println(list.getCount());
//        list.all();
//        list.reverse();
//        list.all();

    }
}
